/**
 * Created by z on 2017/4/18.
 */



let imgUpload = $('<input>', {type: 'file', accept: 'image/*'});


$(function () {

    $('body').append(imgUpload);

});


imgUpload.on({change: imgUploadChangeHandle});


function imgUploadChangeHandle() {


    if (0 === this.files.length) {

        return false;
    }

    if (!/image\/\w+/.test(this.files[0].type)) {
        alert("请选择图片上传！");
        return false;
    }

    img2base(this.files[0]);

}


function img2base(img) {

    let promise = new Promise(function (resolve, reject) {
        if (window.FileReader) {
            let fr = new FileReader();

            fr.readAsDataURL(img);


            fr.addEventListener('load', () => {
                resolve($('<img>', {src: event.target.result, width: '100%'}));

            });

            fr.addEventListener('error', () => {
                reject();
            })


        }
        else {
            alert("Not supported by your browser!");
            reject();
        }
    });

    promise.then(function (value) {
        $('body').append(value)
    }, function () {

    })


}
